//
//  DiscoveryViewController.m
//  礼物
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "DiscoveryViewController.h"
#import "CategoryModel.h"
#import "CategoryView.h"
#import "Categories.h"
#import "SearchViewController.h"
#import "SearchDetailViewController.h"

@interface DiscoveryViewController ()<SendDelegate>
@property(nonatomic,strong)NSMutableDictionary *allDic;
@property(nonatomic,strong)CategoryView *categoryV;
@property(nonatomic,strong)BaseView *searchView;
@end

@implementation DiscoveryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"发现";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-sousuo.png"] style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItem)];
    self.view.backgroundColor=[UIColor whiteColor];
    self.allDic = [NSMutableDictionary dictionary];
    [self getAFNetWorkingData];
   
    
    
    //图片
    UIImageView *aImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5*HSCALE, 30*HSCALE, 30*HSCALE)];
    aImageView.image = [UIImage imageNamed:@"66666"];
    [self.view addSubview:aImageView];
 
        //标签
    BaseLabel *label = [[BaseLabel alloc] initWithFrame:CGRectMake(10 + aImageView.frame.size.width + 10, 0, WIDTH / 2, HEIGHT / 18)];
    label.text = @"选礼神器";
    label.textColor = [UIColor blackColor];
    [self.view addSubview:label];
   
    self.searchView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40 *HSCALE)];
    self.searchView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.searchView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.searchView addGestureRecognizer:tap];
    
    
    self.categoryV = [[CategoryView alloc] initWithFrame:CGRectMake(0, self.searchView.frame.size.height, WIDTH, HEIGHT-49-64 - self.searchView.frame.size.height)];
    self.categoryV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.categoryV];
    self.categoryV.myDelegate = self;


}
-(void)rightBarButtonItem
{
    SearcherViewController *searchVC=[[SearcherViewController alloc]init];
    searchVC.navigationItem.title=@"搜索";
    searchVC.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:searchVC animated:YES];
}
-(void)sendDelegate:(CategoryModel *)category
{
    SearchDetailViewController *searchDetailVC = [[SearchDetailViewController alloc] init];
    searchDetailVC.category = category;
    NSLog(@"category = %@",category);
     searchDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchDetailVC animated:YES];

}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"hahha");
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    
    //push到详情的时候让tabBar(工具栏)消失
    searchVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchVC animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)getAFNetWorkingData
{
    NSString *urlString = @"http://api.liwushuo.com/v2/item_categories/tree";
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
//        [self.categoryModelArray removeAllObjects];
        
        NSMutableDictionary *dictionary = responseObject;
        NSArray *array = [[dictionary objectForKey:@"data"] objectForKey:@"categories"];
        //请求大model的数据
       
        for (NSMutableDictionary *dic in array)
        {
            Categories *categories = [[Categories alloc] init];
            [categories setValuesForKeysWithDictionary:dic];
            
            categories.subcategories = [NSMutableArray array];
            NSArray *subcategories = [dic objectForKey:@"subcategories"];
            for (NSDictionary *smallDic in subcategories) {
                CategoryModel *categoryModel = [[CategoryModel alloc] init];
                [categoryModel setValuesForKeysWithDictionary:smallDic];
                categoryModel.category_id = [smallDic objectForKey:@"id"];
                [categories.subcategories addObject:categoryModel];
            }
          
            [self.allDic setObject:categories forKey:categories.name];
            
            
        }
        
        
        //            menu_width;
        //            menu_button_height;
        //           categories_size;

        self.categoryV.menu_width = 80*WSCALE;
        self.categoryV.menu_button_height = 50*HSCALE;
        self.categoryV.allDic = self.allDic;
        
        

    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             NSLog(@"error = %@",error);
             //本地缓存
         }];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
